package com.yjz.study.adapter.detail

import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.yjz.study.R
import com.yjz.study.http.entity.res.GoodsDetailModel
import com.yjz.ui.recycler.item.HiDataItem
import com.yjz.ui.recycler.item.HiViewHolder
import com.yjz.ui.view.InputItemLayout

/**
 *作用 : 商品详情之商品描述
 *作者 : yjz
 *时间 : 2025/5/15 14:45
 */
class GoodsDetailAttrItem(val detailModel: GoodsDetailModel) : HiDataItem<GoodsDetailModel, HiViewHolder>() {
    override fun onBindData(holder: HiViewHolder, position: Int) {
        val context = holder.itemView.context ?: return
        val goodAttr = detailModel.goodAttr
        val attrContainer = holder.itemView.findViewById<LinearLayout>(R.id.attr_container)
        if (!goodAttr.isNullOrEmpty()) {
            val iterator = goodAttr.iterator()
            var index = 0
            while (iterator.hasNext()) {
                val attr = iterator.next()
                val entries = attr.entries
                val key = entries.first().key
                val value = entries.first().value

                val attrItemView: InputItemLayout = if (index < attrContainer.childCount) {
                    attrContainer.getChildAt(index)
                } else {
                    LayoutInflater.from(context)
                        .inflate(
                            R.layout.layout_goods_detail_attr_item_detail,
                            attrContainer,
                            false
                        )
                } as InputItemLayout

                attrItemView.getEditText().hint = value
                attrItemView.getEditText().isEnabled = false
                attrItemView.getTitleText().text = key

                if (attrItemView.parent == null) {
                    attrContainer.addView(attrItemView)
                }
                index++
            }
            if (index > 0) {
                attrContainer.visibility = View.VISIBLE
            }
        } else {
            attrContainer.visibility = View.GONE
        }

        detailModel.goodDescription?.let {
            val attrDesc = holder.findViewById<TextView>(R.id.attr_desc)
            attrDesc?.visibility = View.VISIBLE
            attrDesc?.text = it
        }
    }

    override fun getItemLayoutRes(): Int {
        return R.layout.layout_goods_detail_attr_item
    }
}